import icon from 'common/topo/topoIcon.js'
import { TOPO_TYPE_TOPO, TOPO_TYPE_BUS } from 'common/resTypeEnum'

function isEmpty(obj) {
    if (obj == null || obj == undefined || obj == '' || $.isEmptyObject(obj)) {
        return true
    }
    return false
}

export default (function() {
    let routetableMenu = {
        label: '路由表',
        icon: icon.routetable,
        action(item, e) {
            console.log('路由表')
        }
    }
    let propertyMenu = {
        label: '设备属性',
        icon: icon.property_icon,
        action(item, e) {
            console.log('设备属性')
        }
    }
    let subtopoMenu = {
        label: '查看子拓扑',
        icon: icon.subtopo_icon,
        action(item, e) {
            console.log('查看子拓扑')
        }
    }
    let assetMenu = {
        label: '资产信息',
        icon: icon.asset_icon,
        action(item, e) {
            console.log('资产信息')
        }
    }
    let performanceMenu = {
        label: '查看主机性能',
        icon: icon.performance_icon,
        action(item, e) {
            console.log('查看主机性能')
        }
    }
    let interfaceMenu = {
        label: '设备面板图',
        icon: icon.interface_icon,
        action(item, e) {
            console.log('设备面板图')
        }
    }
    let editLinkMenu = {
        label: '修改连接信息',
        icon: icon.edit_icon,
        action(item, e) {
            console.log('修改连接信息')
        }
    }
    let editDeviceMenu = {
        label: '修改设备信息',
        icon: icon.edit_icon,
        action(item, e) {
            console.log('修改设备信息')
        }
    }
    let importDeviceMenu = {
        label: '导入设备',
        icon: icon.import_icon,
        action(item, e) {
            console.log('导入设备')
        }
    }
    let addLinkMenu = {
        label: '新增连接',
        icon: icon.add_icon,
        action(item, e) {
            console.log('新增连接')
        }
    }
    let delEleMenu = {
        label: '删除',
        icon: icon.delete_icon,
        action(item, e) {
            console.log('删除')
        }
    }

    let pingMenu = {
        label: 'Ping',
        icon: icon.command_line,
        action(item, e) {
            console.log('Ping')
        }
    }

    let tracerouteMenu = {
        label: 'Traceroute',
        icon: icon.command_line,
        action(item, e) {
            console.log('Traceroute')
        }
    }
    let telentMenu = {
        label: 'Telent',
        icon: icon.command_line,
        action(item, e) {
            console.log('Telent')
        }
    }
    let contextmenu = new ht.widget.ContextMenu();
    contextmenu.enableGlobalKey();
    let data = {
        //右键菜单
        contextmenu: contextmenu,
        //以下是可选菜单项
        routetableMenu: routetableMenu,
        propertyMenu: propertyMenu,
        subtopoMenu: subtopoMenu,
        assetMenu: assetMenu,
        performanceMenu: performanceMenu,
        interfaceMenu: interfaceMenu,
        editLinkMenu: editLinkMenu,
        editDeviceMenu: editDeviceMenu,
        importDeviceMenu: importDeviceMenu,
        addLinkMenu: addLinkMenu,
        delEleMenu: delEleMenu,
        pingMenu: pingMenu,
        tracerouteMenu: tracerouteMenu,
        telentMenu: telentMenu,
        //菜单初始化方法 使用时必须调用
        init: function(topoType, graphView, currentCMData) {
            const type = topoType
            const view = graphView
            data.contextmenu.beforeShow = function(e) {
                if (!isEmpty(currentCMData) && !isEmpty(currentCMData['mouseupEventPos'])) {
                    console.log(e)
                    console.log('右键坐标  x:' + e.originEvent.x + ',y:' + e.originEvent.y)
                    currentCMData.mouseupEventPos.x = e.originEvent.x;
                    currentCMData.mouseupEventPos.y = e.originEvent.y;
                }
                let menu = []
                let ele = view.getDataAt(e)
                    //普通的拓扑图
                if (type == TOPO_TYPE_TOPO) {
                    if (ele instanceof ht.Node) {
                        menu.push(data.editDeviceMenu)
                        menu.push("separator")
                        menu.push(data.performanceMenu)
                        menu.push("separator")
                            // menu.push(data.interfaceMenu)
                        menu.push(data.propertyMenu)
                        menu.push("separator")
                        menu.push(data.routetableMenu)
                        menu.push(data.assetMenu)
                        menu.push("separator")
                        menu.push(data.pingMenu)
                        menu.push(data.tracerouteMenu)
                            // menu.push(data.telentMenu)
                        menu.push("separator")
                        menu.push(data.delEleMenu)
                    } else if (ele instanceof ht.Edge) {
                        // menu.push(data.editLinkMenu)
                        menu.push(data.delEleMenu)
                    } else if (ele instanceof ht.Group) {
                        menu.push(data.delEleMenu)
                    } else {
                        menu = []
                            // menu.push(data.importDeviceMenu)
                            // menu.push(data.addLinkMenu)
                    }
                } else if (type == TOPO_TYPE_BUS) { //业务拓扑图
                    if (ele instanceof ht.Node) {
                        // menu.push(data.interfaceMenu)
                        menu.push(data.performanceMenu)
                        menu.push("separator")
                            // menu.push(data.propertyMenu)
                        menu.push(data.editDeviceMenu)
                        menu.push(data.addLinkMenu)
                        menu.push("separator")
                        menu.push(data.delEleMenu)
                    } else if (ele instanceof ht.Edge) {
                        menu.push(data.delEleMenu)
                    } else if (ele instanceof ht.Group) {
                        menu.push(data.delEleMenu)
                    } else {
                        menu.push(data.importDeviceMenu)
                        menu.push(data.addLinkMenu)
                    }
                }
                contextmenu.setItems(menu)
            }
            contextmenu.addTo(graphView.getView())
        }
    }
    return data
})()